<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of find_path</title>
  <meta name="keywords" content="find_path">
  <meta name="description" content="find_path(g,u,v) --- find a shortest path from u to v">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; find_path.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>find_path
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>find_path(g,u,v) --- find a shortest path from u to v</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function p = find_path(g,u,v) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> find_path(g,u,v) --- find a shortest path from u to v</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>	neighbors(g,v) --- neighbors of v as a list.</li><li><a href="nv.html" class="code" title="function n = nv(g)">nv</a>	nv(g) --- number of vertices in g</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="bridges.html" class="code" title="function blist = bridges(g,algo)">bridges</a>	bridges(g,algo) --- find all cut edges in g</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function p = find_path(g,u,v)</a>
0002 <span class="comment">% find_path(g,u,v) --- find a shortest path from u to v</span>
0003 
0004 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0005 
0006 <span class="keyword">if</span> (u&lt;1) || (u&gt;n) || (v&lt;1) || (v&gt;n)
0007     p = [];
0008     <span class="keyword">return</span>
0009 <span class="keyword">end</span>
0010 
0011 <span class="keyword">if</span> u==v
0012     p = u;
0013     <span class="keyword">return</span>
0014 <span class="keyword">end</span>
0015 
0016 q_init(n+1); 
0017 track = zeros(1,n);
0018 track(v) = v;
0019 
0020 q_push(v); 
0021 
0022 <span class="keyword">while</span>(q_size &gt; 0)
0023     t = q_pop_front;
0024     <span class="keyword">if</span> t==u
0025         <span class="keyword">break</span>
0026     <span class="keyword">end</span>
0027     push_list = <a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>(g,t);
0028     <span class="keyword">for</span> s = push_list
0029         <span class="keyword">if</span> (track(s) == 0)
0030             track(s) = t;
0031             q_push(s);
0032         <span class="keyword">end</span>
0033     <span class="keyword">end</span>
0034 <span class="keyword">end</span>
0035 
0036 <span class="keyword">if</span> track(u) == 0
0037     p = [];
0038     <span class="keyword">return</span>
0039 <span class="keyword">end</span>
0040 
0041 p = [];
0042 last = u;
0043 <span class="keyword">while</span> (last ~= v)
0044     p = [p,last];
0045     last = track(last);
0046 <span class="keyword">end</span>
0047 p = [p,v];
0048 
0049 
0050 q_init(1);</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>